<?php

include_once 'Expression.php';
include_once 'IterationStatement.php';

/**
 * Represents while and do..while statements
 * 
 * @package UC_PHPCodeModel
 * @author Uros Calakovic 
 * @example ../Examples/WhileStatementExample.php
 */

class WhileStatement extends IterationStatement
{
    /**
     * The WhileStatement constructor
     * 
     * @param Expression $TestExpression The expression to test for loop termination
     * @param StatementCollection $Statements The statements to be executed
     * @param type $isDoWhile True if this is a do..while loop
     */
    
    public function __construct(Expression $TestExpression = null, 
        StatementCollection $Statements = null, $isDoWhile = false)
    {
        parent::__construct($Statements);
        
        is_null($TestExpression) ?
            $this->TestExpression = new Expression() :
            $this->set_TestExpression($TestExpression);
        
        $this->set_isDoWhile($isDoWhile);
    }
    
    /**
     * Used for making private / protected variables 
     * accessible from outside the class.
     * 
     * @param string $name Name of the field to be accessed
     * @param mixed $value The value the field should be set to, usually a collection.
     * @see CodeObject::__set()
     */
    
    public function __set($name, $value)
    {
        parent::__set($name, $value);
        
        if($name == 'TestExpression')
        {
            $this->set_TestExpression($value);
        }
        
        if($name == 'isDoWhile')
        {
            $this->set_isDoWhile($value);
        }
    }
    
    /**
     * Used for making private / protected variables 
     * accessible from outside the class.
     * 
     * @param string $name Name of the field to be accessed
     * @return mixed The field that $name specified, usually a collection
     * @see CodeObject::__get()
     */
    
    public function __get($name)
    {
        $ret = parent::__get($name);
        
        if($ret != null)
        {
            return $ret;
        }
        
        if($name == 'TestExpression')
        {
            return $this->get_TestExpression();
        }
        
        if($name == 'isDoWhile')
        {
            return $this->get_isDoWhile();
        }
    }
    
    /**
     * The $TestExpression getter method
     * @return Expression 
     */
    
    public function get_TestExpression()
    {
        return $this->TestExpression;
    }

    /**
     * The $TestExpression setter method
     * @param Expression $TestExpression 
     * 
     * @assert (123) throws PHPUnit_Framework_Error
     * @assert ('name') throws PHPUnit_Framework_Error
     * @assert (new stdClass()) throws PHPUnit_Framework_Error 
     */
    
    public function set_TestExpression(Expression $TestExpression)
    {
        $this->TestExpression = $TestExpression;
    }

    /**
     * The $isDoWhile getter method
     * @return bool 
     */
    
    public function get_isDoWhile()
    {
        return $this->isDoWhile;
    }

    /**
     * The $isDoWhile setter method
     * @param bool $isDoWhile
     * @throws InvalidArgumentException 
     * 
     * @assert (123) throws InvalidArgumentException
     * @assert ('name') throws InvalidArgumentException
     * @assert (new stdClass()) throws InvalidArgumentException
     */
    
    public function set_isDoWhile($isDoWhile)
    {
        if(is_bool($isDoWhile))
        {
            $this->isDoWhile = $isDoWhile;
        }
        else
        {
            throw new InvalidArgumentException(
                'The value must be boolean.');
        }
    }

    /**
     * The expression to test for loop termination
     * @var Expression 
     */
    
    private $TestExpression;
    
    /**
     * True if this is a do..while loop
     * @var bool 
     */
    
    private $isDoWhile = false;
}

?>
